package com.cps.match.xmlparser;

import java.util.ArrayList;

import com.cps.match.sdo.MerchantMatch;
import com.cps.match.sdo.PrincipalMatch;
import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;

public class ConvertMerchantMatch implements Converter {

	@SuppressWarnings("rawtypes")
	@Override
	public boolean canConvert(Class arg0) {
		return arg0.equals(MerchantMatch.class);
	}

	@Override
	public void marshal(Object obj, HierarchicalStreamWriter writer, MarshallingContext ctx) {

	}

	@Override
	public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext ctx) {
		MerchantMatch merchantMatch = new MerchantMatch();
		while (reader.hasMoreChildren()) {

			reader.moveDown();
			String nodeName = reader.getNodeName();
			String value = reader.getValue();
			if(nodeName.equals("PrincipalMatch")) {
				PrincipalMatch prin = (PrincipalMatch)ctx.convertAnother(merchantMatch.getPrincipalMatch(), PrincipalMatch.class);
				if(merchantMatch.getPrincipalMatch()==null) {
					merchantMatch.setPrincipalMatch(new ArrayList<PrincipalMatch>());
				}
				merchantMatch.getPrincipalMatch().add(prin);
			} else {
				MatchValueHelper.assignValue(merchantMatch, reader.getNodeName(), value);
			}
			reader.moveUp();

		}
		return merchantMatch;

	}

}
